<?php
/**
 * Created by PhpStorm
 * Author Hui
 * Date 2024/5/19   0:22
 */

namespace designpatterns;

class Facade
{
    /**
     * 是否永远创建新实例
     * @var bool
     */
    protected static $alwaysNewInstance;

    protected static function createFacade(string $class = '', array $args = [],bool $newInstance = false)
    {
        $class = $class ?: static::class;
        $facadeClass = static::getFacadeClass();

        if($facadeClass){
            $class = $facadeClass;
        }

        if(static::$alwaysNewInstance){
            $newInstance = true;
        }

        //从容器中获取对应模型
        return Container::getInstance()->make($class,$args,$newInstance);
    }

    /**
     * 获取当前Facade对应名字
     * @access protected
     * @return string
     */
    protected static function getFacadeClass(){}


    /**
     * 实际调用类，所有类用到Facade都是启用这个方法
     * @param $method
     * @param $arguments
     * @return false|mixed
     */
    public static function __callStatic($method, $arguments)
    {
        return call_user_func_array([static::createFacade(),$method],$arguments);
    }
}